/****************************************************************************
 * taskapp
 * Tested MCU 
 * Owner : apleilx
 * **************************************************************************/
#define _TEMPTASK_MODULE_
#include "temperature.h"
#include "systimer.h"
#include "pincfg.h"
#include "systask.h"
#include "taskapp.h"
#include "appcfg.h"

#include "tempctr.h"

#include "math.h"
#include "mbinputdatacfg.h"
void CO2Cal(void);
void NtcTempCal1(void);
void NtcTempCal(void);
void ErrHandle(void);
int16_t  DispTempGet(TEMP_OBJ_Type* TempObj);
void TempADSet(TEMP_OBJ_Type* TempObj ,int16_t AD_Val);
// 3k , 75 ,3k,47k,SGM722 -60-400度。 5000毫伏    100欧姆  247.08欧姆
const TEMP_TABLE_Type Pt100_400_Table[] = {
    {-3513, -620},
    {-3283, -580},
    {-3053, -540},
    {-2823, -500},
    {-2594, -460},
    {-2366, -420},
    {-2138, -380},
    {-1911, -340},
    {-1684, -300},
    {-1458, -260},
    {-1233, -220},
    {-1007, -180},
    {-783, -140},
    {-559, -100},
    {-335, -60},
    {-112, -20},
    {110, 20},
    {332, 60},
    {554, 100},
    {775, 140},
    {996, 180},
    {1216, 220},
    {1436, 260},
    {1655, 300},
    {1874, 340},
    {2092, 380},
    {2310, 420},
    {2527, 460},
    {2744, 500},
    {2960, 540},
    {3176, 580},
    {3392, 620},
    {3607, 660},
    {3821, 700},
    {4035, 740},
    {4249, 780},
    {4462, 820},
    {4675, 860},
    {4887, 900},
    {5099, 940},
    {5310, 980},
    {5521, 1020},
    {5731, 1060},
    {5941, 1100},
    {6150, 1140},
    {6359, 1180},
    {6568, 1220},
    {6776, 1260},
    {6984, 1300},
    {7191, 1340},
    {7397, 1380},
    {7604, 1420},
    {7809, 1460},
    {8015, 1500},
    {8220, 1540},
    {8424, 1580},
    {8628, 1620},
    {8832, 1660},
    {9035, 1700},
    {9238, 1740},
    {9440, 1780},
    {9642, 1820},
    {9843, 1860},
    {10044, 1900},
    {10244, 1940},
    {10444, 1980},
    {10644, 2020},
    {10843, 2060},
    {11042, 2100},
    {11240, 2140},
    {11438, 2180},
    {11635, 2220},
    {11832, 2260},
    {12029, 2300},
    {12225, 2340},
    {12421, 2380},
    {12616, 2420},
    {12811, 2460},
    {13006, 2500},
    {13200, 2540},
    {13393, 2580},
    {13586, 2620},
    {13779, 2660},
    {13971, 2700},
    {14163, 2740},
    {14355, 2780},
    {14546, 2820},
    {14737, 2860},
    {14927, 2900},
    {15117, 2940},
    {15306, 2980},
    {15495, 3020},
    {15684, 3060},
    {15872, 3100},
    {16060, 3140},
    {16247, 3180},
    {16434, 3220},
    {16620, 3260},
    {16807, 3300},
    {16992, 3340},
    {17178, 3380},
    {17363, 3420},
    {17547, 3460},
    {17731, 3500},
    {17915, 3540},
    {18098, 3580},
    {18281, 3620},
    {18464, 3660},
    {18646, 3700},
    {18827, 3740},
    {19009, 3780},
    {19190, 3820},
    {19370, 3860},
    {19550, 3900},
    {19730, 3940},
    {19909, 3980},
    {20088, 4020},
    {20267, 4060},
    {20445, 4100},
    {20623, 4140},
    {20800, 4180},
    {20977, 4220},
    {INT16_MAX, INT16_MAX}};

void PressCal(void);

/*****************************************************************************/ /*!
*
* @brief   motor task.
*         
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void TempTask(void)
{
  
}

/*****************************************************************************/ /*!
*
* @brief   temperature par init.
*                 
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void TempInit()
{
    Temp.Err = 0;
    Temp.ADVal = 0;
    Temp.Temp = 0;
    Temp.TempDis = 0;
    Temp.TempReal = 0;
}
 /*****************************************************************************//*!
*
* @brief   temperature calculate .
*          ad val to temperature       
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
#define RES_25C     50000.0f
#define AD_MAX      8192U
#define AD_LIMITE_H 7950U
#define AD_LIMITE_L 200U
#define RES_REF     20000U
#define TEMP_KREF   273.15f
#define TEMP_K25C   298.15f
#define NTC_B       3950.0f

void NtcTempCal()
{
    /*------------------------------------------------------------------------*
    * B = Ln(Rt1/Rt2)/(1/T1 - 1/T2)
    *
    * T2 = B*T1/(B - T1*Ln(Rt1-Rt2))
    *
    *
    * Adx/Rx = (AdMax - Adx)/R0
    *
    *------------------------------------------------------------------------*/
    float fltt;
    uint16_t Adcc;
    uint32_t u32t;
    uint16_t AdcVal0 =  AdcVal[0];
    
    if(AdcVal0 > AD_LIMITE_H)
    {
        Adcc = AD_LIMITE_H;
        sys_st.err.air_temp_open = 1;
        sys_st.err.air_temp_short = 0;
    }
    else if(AdcVal0 < AD_LIMITE_L)
    {
        Adcc = AD_LIMITE_L;
        sys_st.err.air_temp_open = 0;
        sys_st.err.air_temp_short = 1;
    }
    else
    {
        Adcc = AdcVal0;
        sys_st.err.air_temp_open = 0;
        sys_st.err.air_temp_short = 0;
    }
    
    u32t  = RES_REF * (uint32_t)Adcc;
    u32t /= AD_MAX  - Adcc;
    fltt   = u32t;                        //??

    fltt = log(RES_25C/fltt);
    fltt = TEMP_K25C*fltt;
    fltt = NTC_B - fltt;
    fltt = NTC_B*TEMP_K25C/fltt;
    fltt -= TEMP_KREF;
    fltt *=10;
    
    sys_st.in.i_air_temp = fltt;
}
/*****************************************************************************//*!
*
* @brief   temperature calculate .
*          ad val to temperature       
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
#define RES_25C1     10000.0f
#define AD_MAX1      (1024*16)
#define AD_LIMITE_H1 (AD_MAX1 - 360)
#define AD_LIMITE_L1 (360)
#define RES_REF1      6800
#define TEMP_KREF1   273.15f
#define TEMP_K25C1   298.15f
#define NTC_B1       3950.0f

void NtcTempCal1()
{
    /*------------------------------------------------------------------------*
    * B = Ln(Rt1/Rt2)/(1/T1 - 1/T2)
    *
    * T2 = B*T1/(B - T1*Ln(Rt1-Rt2))
    *
    *
    * Adx/Rx = (AdMax - Adx)/R0
    *
    *------------------------------------------------------------------------*/
    float fltt1;
    uint16_t Adcc1;
    uint32_t u32t1;
    uint16_t AdcVal1 =  AdcVal[1];
    
    if(AdcVal1 > AD_LIMITE_H1)
    {
        Adcc1 = AD_LIMITE_H1;
        sys_st.err.air_temp_open1 = 1;
        sys_st.err.air_temp_short1 = 0;
    }
    else if(AdcVal1 < AD_LIMITE_L1)
    {
        Adcc1 = AD_LIMITE_L1;
        sys_st.err.air_temp_open1 = 0;
        sys_st.err.air_temp_short1 = 1;
    }
    else
    {
        Adcc1 = AdcVal1;
        sys_st.err.air_temp_open1 = 0;
        sys_st.err.air_temp_short1 = 0;
    }
    
    u32t1  = RES_REF1 * (uint32_t)Adcc1;
    u32t1 /= AD_MAX1  - Adcc1;
    fltt1   = u32t1;                        //??

    fltt1 = log(RES_25C1/fltt1);
    fltt1 = TEMP_K25C1*fltt1;
    fltt1 = NTC_B1 - fltt1;
    fltt1 = NTC_B1*TEMP_K25C1/fltt1;
    fltt1 -= TEMP_KREF1;
    fltt1 *=10;
    
    sys_st.in.i_air_temp1 = fltt1;
    ErrHandle();
}
/*****************************************************************************//*!
* @brief   error handle .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SystemRun(void);
void ErrHandle()
{  
 if(PowerUpTimeCnt < 50)
     {
          sys_st.err.air_temp_high = 0;
         sys_st.err.air_temp_low=0;
        return ;
     }
    
     {
        if((sys_st.in.i_air_temp1 > 700)||(sys_st.err.air_temp_open1)||(sys_st.err.air_temp_short1))
		{
                 if(bPulse_1s)
                 {
                      TEMPDELAYTimeCnt++;
                 }
                 if(TEMPDELAYTimeCnt>=5)
                 {
                       sys_st.err.air_temp_high = 1;
                 }
			
		}
        else  if(sys_st.in.i_air_temp1 <690)
        {
             sys_st.err.air_temp_high = 0;
             TEMPDELAYTimeCnt=0;
        }
    }

    if((sys_st.err.air_temp_low)||(sys_st.err.air_temp_high))
    {
            SpeakerNoticeTimeCnt=10;
    }
    else
    {
        SpeakerNoticeTimeCnt=0;
    }
    SystemRun();
    
}
